<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="85px"
      class="globalSerchFormStyle"
      :rules="rules"
    >
      <el-row>
        <el-col :span="6" v-if="authUser">
          <el-form-item label="油企名称" prop="enterpriseId" size="small">
            <el-select
              style="width: 180px"
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col
          :span="6"
          v-if="[null, '', '1'].includes($store.state.user.userLevel)"
        >
          <el-form-item label="油站名称" prop="stationId" size="small">
            <el-select
              style="width: 180px"
              v-model="queryParams.stationId"
              :clearable="true"
              filterable
              placeholder="请选择油站名称"
            >
              <el-option
                v-for="item in stationList"
                :key="item.stationId"
                :label="item.stationName"
                :value="item.stationId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="订单号" prop="orderNo">
            <el-input
              style="width: 180px"
              v-model.trim="queryParams.orderNo"
              placeholder="请输入订单号"
              clearable
              @keyup.enter.native="handleQuery"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户手机号" prop="mobilePhone">
            <el-input
              style="width: 180px"
              v-model.trim="queryParams.mobilePhone"
              placeholder="请输入客户手机号"
              clearable
              @keyup.enter.native="handleQuery"
              size="small"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-show="collapse">
        <el-row>
          <el-col :span="6">
            <el-form-item label="变动类型" prop="recordType" size="small">
              <el-select
                style="width: 180px"
                v-model="queryParams.recordType"
                placeholder="请选择变动类型"
                clearable
              >
                <el-option :value="1" label="增加积分"></el-option>
                <el-option :value="2" label="减少积分"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="变动来源" prop="action" size="small">
              <el-select
                style="width: 180px"
                v-model="queryParams.action"
                placeholder="请选择变动来源"
                clearable
              >
                <el-option
                  v-for="(action, index) in changeSource"
                  :key="index"
                  :value="action.value"
                  :label="action.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="变动时间" prop="createdDate" size="small">
              <el-date-picker
                size="small"
                v-model="dateRangeCreatedDate"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 180px"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>

    <el-row
      class="global_el_rowStyle"
      :gutter="10"
      style="
        border-top: none;
        border-bottom: 1px solid #f5f5f5;
        margin-top: 0px;
      "
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          @click="resetQuery"
          size="mini"
          icon="el-icon-refresh"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          id="closeSearchBtn"
          @click="
            (closeSearch) => {
              collapse = !collapse;
            }
          "
        >
          {{ collapse ? "收起" : "展开更多" }}
          <i :class="collapse ? 'el-icon-arrow-up ' : 'el-icon-arrow-down'"></i>
        </el-button>
      </el-col>
    </el-row>
    <div style="width: 80%; margin: 20px 0px">
      <el-row :gutter="20">
        <el-col :span="5">
          <div class="order-fz">
            积分增加合计：<span class="order-text-color">{{
              pointSum.integralIncrease
            }}</span>
          </div>
        </el-col>
        <el-col :span="5">
          <div class="order-fz">
            积分消减合计：<span class="order-text-color">{{
              pointSum.integralDecrease
            }}</span>
          </div>
        </el-col>
        <el-col :span="5">
          <div class="order-fz">
            积分变动合计：<span class="order-text-color"
              >{{ pointSum.integralTotal }}
            </span>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-table
      stripe
      style="width: 100%"
      :data="dataList"
      ref="tableRef"
      v-loading="loading"
    >
      <af-table-column
        v-if="authUser"
        label="油企名称"
        align="left"
        width="250"
        show-overflow-tooltip
        prop="enterpriseName"
      />
      <af-table-column
        v-if="[null, '', '1'].includes($store.state.user.userLevel)"
        label="油站名称"
        align="left"
        width="250"
        show-overflow-tooltip
        prop="stationName"
      />
      <af-table-column
        width="200"
        label="客户名称"
        align="left"
        prop="blogNickName"
      />
      <af-table-column width="200" label="客户手机号" prop="mobilePhone" />
      <af-table-column
        width="250"
        label="变动时间"
        align="left"
        prop="createTime"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </af-table-column>
      <af-table-column
        label="变动类型"
        align="left"
        prop="recordType"
        width="200"
      />
      <af-table-column label="变动来源" align="left" prop="action" width="200">
      </af-table-column>
      <af-table-column
        label="积分增减数"
        align="left"
        prop="integral"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.integral > 0
              ? `+${scope.row.integral}`
              : scope.row.integral
          }}</span>
        </template>
      </af-table-column>
      <af-table-column width="250" label="订单号" align="left" prop="orderNo" />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { detailList } from "@/api/integral/details";
import { cloneDeep } from "lodash";
import { enterpriseList } from "@/api/station/order";
import { listEnterpriseStation, getStationByEnterprise } from "@/api/dept";
import getUserProfileName from "@/utils/getUserProfile";
import { mapState } from "vuex";
import { template } from "lodash";
export default {
  name: "Point_Details",
  data() {
    return {
      changeSource: [
        { value: 1, label: "兑换商品" },
        { value: 2, label: "加油" },
        { value: 3, label: "充值赠送" },
        { value: 4, label: "退款" },
        { value: 5, label: "手动添加" },
        { value: 6, label: "手动消减" },
        { value: 7, label: "导入" },
        // {value:8, label:'积分抵现'},
        { value: 9, label: "非油商品购买" },
      ],
      dataList: [],
      collapse: false,
      //创建订单时间间隔
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        mobilePhone: null,
        stationId: null,
        enterpriseId: null,
        recordType: null,
        action: null,
      },
      pointSum: {
        integralDecrease: null,
        integralIncrease: null,
        integralTotal: null,
      },
      // 表单校验
      rules: {},
      stationList: [],
      enterpriseList: [],
      loading: false,
    };
  },
  async created() {
    this.getList();
    this.getEnterpriseList();
    // 如果当前是油企账号，则获取该企业下的油站
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.getStationList(this.presentEnterprise.enterpriseId);
    } else {
      this.getStationList();
    }
  },
  methods: {
    // 根据油企获取油站
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },

    /** 查询订单支付列表 */
    async getList() {
      this.loading = true;
      let queryParams = cloneDeep(this.queryParams);

      const data = this.addDateRange(
        {
          ...queryParams,
        },
        this.dateRangeCreatedDate
      );

      // 列表请求
      try {
        const res = await detailList(data);
        const { total, list } = res.data;
        this.total = total;
        this.dataList = list.integralData;
        this.pointSum.integralTotal = list.integralTotal;
        this.pointSum.integralDecrease = list.integralDecrease;
        this.pointSum.integralIncrease = list.integralIncrease;
      } finally {
        this.loading = false;
      }
    },

    async getStationList(val) {
      // 判断是不是admin账号
      const isDelete = await getUserProfileName();
      if (isDelete && val) {
        // 是
        listEnterpriseStation({ enterpriseId: val }).then((res) => {
          this.stationList = res;
        });
      } else {
        // 不是
        const res = await getStationByEnterprise();
        this.stationList = res.data;
      }
    },

    /** 搜索按钮操作 */
    async handleQuery() {
      this.queryParams.pageNum = 1;
      await this.$refs["queryForm"].validate();
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
  computed: {
    ...mapState({
      userLevel: (state) => state.user.userLevel,
    }),
    authUser() {
      let roles = this.$store.state.user.roles;
      if (roles.includes("admin")) {
        return true;
      } else {
        return false;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  margin-top: -15px;
}

.amt_sty {
  ::v-deep .el-input__inner {
    width: 95%;
  }
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 8px;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}

.order-fz {
  font-size: 13px;
}

.order-text-color {
  color: #1990ff;
}
</style>
